package com.cy.store.controller;

import com.cy.store.controller.ex.*;
import com.cy.store.service.ex.*;
import com.cy.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.rmi.ServerException;

/**
 * 表示控制层类的基类
 */
@RestController
public class BaseController {
    public static final int OK = 200;//操作成功的状态码

    /**
     * 请求处理方法，这个方法的返回值就是需要传递给前端的数据
     * 自动将异常对象传递给此方法的参数列表上
     * 项目产生异常会被统一拦截到此方法中，这个方法此时就是充当的是请求处理方法，方法的返回值直接给到前端
     */
    @ExceptionHandler({ServiceException.class, FileUploadException.class})//用于统一处理抛出的异常
    public JsonResult<Void> handelException(Throwable e) throws IOException {
        JsonResult<Void> result = new JsonResult<>(e);
        if (e instanceof UsernameDuplicatedException) {
            result.setState(4000);
            result.setMessage("用户名已经被占用");
        } else if (e instanceof UserNotFoundException) {
            result.setState(4001);
            result.setMessage("用户名不存在");
        } else if (e instanceof PasswordNotMatchException) {
            result.setState(4002);
            result.setMessage("用户名或密码有误");
        } else if (e instanceof AddressCountLimitException) {
            result.setState(4003);
        } else if (e instanceof AddressNotFoundException) {
            result.setState(4004);
            result.setMessage("收货地址不存在");
        } else if (e instanceof AccessDeniedException) {
            result.setState(4005);
            result.setMessage("非法访问");
        } else if (e instanceof ProductNotFoundException) {
            result.setState(4006);
            result.setMessage("未查询到相关商品信息");
        } else if (e instanceof CartNotFoundException) {
            result.setState(4007);
            result.setMessage("购物车数据不存在");
        } else if (e instanceof OpeException) {
            result.setState(4008);
            result.setMessage("运算符异常");
        } else if (e instanceof OrderNotFoundException) {
            result.setState(4009);
            result.setMessage("订单信息未找到");
        } else if (e instanceof InsertException) {
            result.setState(5000);
            result.setMessage("未知异常");
        } else if (e instanceof UpdateException) {
            result.setState(5001);
            result.setMessage("更新异常");
        } else if (e instanceof UpdateException) {
            result.setState(5002);
            result.setMessage("删除异常");
        } else if (e instanceof FileEmptyException) {
            result.setState(6000);
            result.setMessage("头像文件为空");
        } else if (e instanceof FileSizeException) {
            result.setState(6001);
            result.setMessage("头像图片过大");
        } else if (e instanceof FileTypeException) {
            result.setState(6002);
            result.setMessage("文件类型不匹配");
        } else if (e instanceof FileStateException) {
            result.setState(6003);
            result.setMessage("文件状态异常");
        } else if (e instanceof FileUploadException) {
            result.setState(6004);
            result.setMessage("文件下载异常");
        }
        return result;
    }

    /**
     * 获取session中的uid
     *
     * @param httpSession
     * @return
     */
    protected final Integer getUidFromSession(HttpSession httpSession) {
        return Integer.valueOf(httpSession.getAttribute("uid").toString());
    }

    /**
     * 获取session中的username
     *
     * @param httpSession
     * @return
     */
    protected final String getUsernameFromSession(HttpSession httpSession) {
        return httpSession.getAttribute("username").toString();
    }

}
